/**
 * Copyright (c) 2009 - 2019, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
/** @file
* @brief Example template project.
* @defgroup nrf_templates_example Example Template
*
*/

#include <stdbool.h>
#include <stdint.h>

#include "nrf.h"
#include "nordic_common.h"
#include "boards.h"

#include "nrf_drv_spi.h"
#include "nrf_delay.h"

#include "ws2812.h"
#include "spi_flash.h"

#include"string.h"
#include "voice_data.h"

const nrf_drv_spi_t m_spi_2 = NRF_DRV_SPI_INSTANCE(2);


#define NRFX_SPI2_SCK_PIN             NRF_GPIO_PIN_MAP(0,28)
#define NRFX_SPI2_MISO_PIN            NRF_GPIO_PIN_MAP(0,27)
#define NRFX_SPI2_MOSI_PIN            NRF_GPIO_PIN_MAP(0,26)
#define NRFX_SX1278_CS_PIN            NRF_GPIO_PIN_MAP(0,25)
#define NRFX_NOR_FLASH_CS_PIN         NRF_GPIO_PIN_MAP(0,17)
#define POWER_ES_EN_PIN               NRF_GPIO_PIN_MAP(0,20) // CODEC PWR



void spi2_init(void)
{
  nrfx_err_t ret_code = 0;

  /* Initialize spi1 instance */
  nrf_drv_spi_config_t spi2_config = NRF_DRV_SPI_DEFAULT_CONFIG;
  spi2_config.frequency      = NRF_DRV_SPI_FREQ_8M;
  spi2_config.ss_pin         = NRFX_SPI_PIN_NOT_USED;
  spi2_config.miso_pin       = NRFX_SPI2_MISO_PIN;
  spi2_config.mosi_pin       = NRFX_SPI2_MOSI_PIN;
  spi2_config.sck_pin        = NRFX_SPI2_SCK_PIN;
  spi2_config.mode           = NRF_DRV_SPI_MODE_0;
  ret_code = nrf_drv_spi_init(&m_spi_2, &spi2_config, NULL, NULL);
}




/******************
 * 失能 CS 引脚
******************/
void spi_flash_cs_high(void)
{
  nrf_gpio_pin_set(NRFX_NOR_FLASH_CS_PIN);
}

void spi_flash_cs_low(void)
{
  nrf_gpio_pin_clear(NRFX_NOR_FLASH_CS_PIN);
}

void spi_flash_tx(uint8_t *p_tx_buffer, uint32_t length)
{
  uint8_t data_buff[512];

  memcpy(data_buff, p_tx_buffer, length);


  if (length>250)
  {
    nrf_drv_spi_transfer(&m_spi_2, data_buff, 250, NULL, 0);
    nrf_drv_spi_transfer(&m_spi_2, data_buff+250, length - 250, NULL, 0);
  }
  else
  {
    nrf_drv_spi_transfer(&m_spi_2, data_buff, length, NULL, 0);
  }
}

void spi_flash_rev(uint8_t *p_rx_buffer,uint32_t length)
{
  uint8_t num_count;
  num_count =  length / 250;
  uint8_t* write_addr = p_rx_buffer;

  if (num_count == 0)
  {
    nrf_drv_spi_transfer(&m_spi_2, NULL, 0, p_rx_buffer, length);
  }
  else
  {
    while (num_count--)
    {
      nrf_drv_spi_transfer(&m_spi_2, NULL, 0, write_addr, 250);
      write_addr +=  250;
    }
    nrf_drv_spi_transfer(&m_spi_2, NULL, 0, write_addr, length%250);
  }
}


ws2812_param_t ws2812_param;
uint32_t flash_id;
uint32_t DeviceID;


voice_data_t voice_data[7] =
{
  [0].data_len = 24,
  [0].mcu_addr = (uint32_t)voice_data_jjhj,   //紧急呼救
  [0].m25q_addr = 0x10000,

  [1].data_len = 27,  //请即刻撤离
  [1].mcu_addr = (uint32_t)voice_data_qjkcl,
  [1].m25q_addr = 0x20000,

  [2].data_len = 32, //嘀嘀嘀井口危险
  [2].mcu_addr = (uint32_t)voice_data_dididijkwx,
  [2].m25q_addr = 0x30000,

  [3].data_len = 30,    // 嘀嘀嘀区域危险
  [3].mcu_addr = (uint32_t)voice_data_dididiqywx,
  [3].m25q_addr = 0x40000,

  [4].data_len = 20,   //嘀请充电
  [4].mcu_addr = (uint32_t)voice_data_diqcd,
  [4].m25q_addr = 0x50000,

  [5].data_len = 27,  // 嘀嘀嘀 禁区勿入
  [5].mcu_addr = (uint32_t)voice_data_dididijqwr,
  [5].m25q_addr = 0x60000,

  [6].data_len = 30,  //  嘀嘀嘀 人员召回
  [6].mcu_addr = (uint32_t)voice_data_dididiryzh,
  [6].m25q_addr = 0x70000,


};


/**
 * @brief Function for application main entry.
 */
int main(void)
{

  nrf_gpio_cfg_output(POWER_ES_EN_PIN);
  nrf_gpio_pin_set(POWER_ES_EN_PIN);
  nrf_gpio_cfg_output(NRFX_SX1278_CS_PIN);
  nrf_gpio_pin_set(NRFX_SX1278_CS_PIN);

  nrf_gpio_cfg_output(NRFX_NOR_FLASH_CS_PIN);
  nrf_gpio_pin_set(NRFX_NOR_FLASH_CS_PIN);

  nrf_delay_ms(10);

  spi2_init();

  SPI_FLASH_Init();
  flash_id = SPI_FLASH_ReadID();
  DeviceID = SPI_FLASH_ReadDeviceID();

  SPI_FLASH_BulkErase();








  for (uint32_t count = 0; count<7; count ++)
  {
    SPI_FLASH_BufferWrite((uint8_t*)(voice_data[count].mcu_addr), voice_data[count].m25q_addr, voice_data[count].data_len*1024);
  }


  ws2812_param.led[0].R = 0xFF;
  ws2812_param.led[0].G = 0;
  ws2812_param.led[0].B = 0;

  ws2812_init (&ws2812_param);

  while (1);



    while (true)
    {
        // Do nothing.
    }
}
/** @} */
